import requests
import os

def crawl_pxu_webpage():
    """
    爬取萍乡学院官网的页面信息，并保存到本地
    """
    # 萍乡学院官网首页URL
    url = "http://www.pxu.edu.cn/"

    # 设置请求头，伪装为浏览器访问
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }

    try:
        # 发送GET请求
        response = requests.get(url, headers=headers, timeout=10)

        # 检查请求是否成功
        if response.status_code == 200:
            # 指定保存路径
            save_path = "D:\\萍乡学院.html"

            # 确保目录存在
            os.makedirs(os.path.dirname(save_path), exist_ok=True)

            # 将内容保存到文件
            with open(save_path, 'wb') as f:
                f.write(response.content)

            print(f"网页已成功保存到 {save_path}")
        else:
            print(f"请求失败，状态码: {response.status_code}")

    except Exception as e:
        print(f"爬取过程中出现错误: {e}")

if __name__ == "__main__":
    crawl_pxu_webpage()